﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;

namespace QLBenhVien
{
    class DAOBenhNhan
    {
        public static bool Add(DBOBenhNhan o)
        {
            DataTable dt = DB_Provider.ReadSchema("select * from HoSoBenhNhan");
            DataRow dr = dt.NewRow();
            foreach (PropertyDescriptor pro in TypeDescriptor.GetProperties(o))
            {
                dr[pro.Name] = pro.GetValue(o);
            }           
            dt.Rows.Add(dr);
            return DB_Provider.Write(dt, "HoSoBenhNhan") > 0;
        }

        public static bool Save(DBOBenhNhan o)
        {
            DataTable dt = DB_Provider.Read("select * from HoSoBenhNhan");
            DataRow dr = dt.Rows.Find(o.id);
            dr.BeginEdit();
            foreach (PropertyDescriptor pro in TypeDescriptor.GetProperties(o))
            {
                dr[pro.Name] = pro.GetValue(o);
            }
            dr.EndEdit();
            return DB_Provider.Write(dt, "HoSoBenhNhan") > 0;
        }

        public static DataTable LoadAll()
        {
            string sql = "select * from HoSoBenhNhan";
            return DB_Provider.Read(sql);
        }

        public static DataTable Search(string hoten, int id)
        {
            string sql = "select * from HoSoBenhNhan where hoten like @hoten and id = @id";
            DataTable dt = new DataTable();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, DB_Provider.conn_str);
                da.SelectCommand.Parameters.Add("@hoten", SqlDbType.NVarChar).Value = '%'+hoten+'%';
                da.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                return null;
            }
            return dt;
        }

        public static DataTable Search(string hoten)
        {
            string sql = "select * from HoSoBenhNhan where hoten like @hoten";
            DataTable dt = new DataTable();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, DB_Provider.conn_str);
                da.SelectCommand.Parameters.Add("@hoten", SqlDbType.NVarChar).Value = '%' + hoten + '%';                
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                return null;
            }
            return dt;
        }

        public static DataTable Search(int id)
        {
            string sql = "select * from HoSoBenhNhan where id = @id";
            DataTable dt = new DataTable();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, DB_Provider.conn_str);                
                da.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                return null;
            }
            return dt;
        }
    }
}
